<?php
/**
 * Created by PhpStorm.
 * User: dy
 * Date: 2019/12/27
 * Time: 9:20
 */
namespace util;

class TimeUtil
{
    protected static $instance = null;
    private function __construct()
    {
    }
    private function __clone()
    {
    }

    public static function instance(){
        if(empty(self::$instance)){
            self::$instance = new static();
        }
        return self::$instance;
    }
    /**
     * 时间戳格式化
     * @param int $time
     * @return string 完整的时间显示
     */
    function format_time(int $time = null,string $format='Y-m-d H:i:s'):string
    {

        if (null === $time) {

            $time = TIME_NOW;
        }

        return date($format, intval($time));
    }

    /**
     * 获取指定日期段内每一天的日期
     * @param \DateTime $startdate 开始日期
     * @param \DateTime $enddate  结束日期
     * @return array
     */
    function get_date_from_range(string $startdate, string $enddate):array
    {

        $stimestamp = strtotime($startdate);
        $etimestamp = strtotime($enddate);

        // 计算日期段内有多少天
        $days = ($etimestamp-$stimestamp)/86400+1;

        // 保存每天日期
        $date = [];

        for($i=0; $i<$days; $i++) {

            $date[] = date('Y-m-d', $stimestamp+(86400*$i));
        }

        return $date;
    }
}